<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Exceptions\SymfonyDisplayer;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        HttpException::class,
        ModelNotFoundException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        return parent::report($e);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
//         public function render($request, Exception $e)
// {
//     if
//     return parent::render($request, $e);
// }


// protected function renderException($e)
// {

//    switch ($e){

//        case ($e instanceof ModelNotFoundException):

//            //自定义处理异常，此处我们返回一个404页面
//            // return view('errors.404');
//            return 111;
//            break;

//        default:

//            //如果异常非ModelNotFoundException，我们返回laravel默认的错误页面
//            return (new SymfonyDisplayer(config('app.debug')))
//                   ->createResponse($e);

//    }
// }
//处理Http响应异常
public function render($request, Exception $e)
{

    switch($e){
        //使用类型运算符 instanceof 判断异常(实例)是否为 ModelNotFoundException
        case ($e instanceof NotFoundHttpException ):


            return $this->renderException($e);
            break;
      case ($e instanceof \Bican\Roles\Exceptions\RoleDeniedException):
        // you can for example flash message, redirect...
         $msg = $e->getMessage();
            \Log::error('Access denied! '.$msg);
       return redirect('/home');


        default:

            return parent::render($request, $e);

    }
}

//处理异常
protected function renderException($e)
{

   switch ($e){

       case ($e instanceof NotFoundHttpException):

           //自定义处理异常，此处我们返回一个404页面

           return redirect()->back();
           break;

       default:

           //如果异常非ModelNotFoundException，我们返回laravel默认的错误页面
           return (new SymfonyDisplayer(config('app.debug')))
                  ->createResponse($e);

   }

}



}
